import 'package:flutter/material.dart';
import 'package:flutter_changdian_football/pages/mine/game_record/desc/game_record_desc_controller.dart';
import 'package:flutter_changdian_football/utils/str_utils.dart';
import 'package:flutter_changdian_football/widget/default_page/custom_empty.dart';
import 'package:flutter_changdian_football/widget/extended_image_slide_widget.dart';
import 'package:flutter_changdian_football/widget/load_state/load_state_layout.dart';
import 'package:flutter_changdian_football/widget/screenutil/custom_screenutil.dart';
import 'package:get/get.dart';

// 比赛记录详情
class GameRecordDescPage extends StatelessWidget {
  const GameRecordDescPage({
    super.key,
    this.clubId = '',
    this.matchId = ''
  });

  final String clubId;
  final String matchId;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          Container(
            width: double.infinity,
            height: double.infinity,
            decoration: const BoxDecoration(
              // gradient: LinearGradient(
              //   begin: Alignment.topCenter,
              //   end: Alignment.bottomCenter,
              //   stops: const [0, 0.1, 1.0],
              //   colors: [
              //     const Color(0xff49935E).withOpacity(0.99),
              //     const Color(0xff4EAE6B),
              //     const Color(0xff518361).withOpacity(0),
              //   ]
              // )
              gradient: LinearGradient(
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter,
                stops: [0, 0.2, 0.4, 1.0],
                colors: [
                  Color(0xff49935E),
                  Color(0xff4EAE6B),
                  Color(0xffF8FAF9),
                  Color(0xffF8FAF9)
                ]
              )
            ),
            child: GetBuilder<GameRecordDescController>(
              init: GameRecordDescController(clubId: clubId, matchId: matchId),
              builder: (gameRecordDescController) {
                return LoadStateLayout(
                  state: gameRecordDescController.state,
                  empty: const CustomEmpty(
                    text: '暂无比赛记录详情',
                  ),
                  success: SafeArea(
                    child: Column(
                      children: [
                        const SizedBox(height: kToolbarHeight,),
                
                        Expanded(
                          child: SingleChildScrollView(
                            padding: EdgeInsets.symmetric(horizontal: 17.w),
                            physics: const BouncingScrollPhysics(),
                            child: Column(
                              children: [
                                SizedBox(height: 40.w,),
                
                                _gameRecordInfoWidget(gameRecordDescController),
                
                                SizedBox(height: 16.w,),
                
                
                                Stack(
                                  children: [
                                    Container(
                                      width: double.infinity,
                                      decoration: BoxDecoration(
                                        borderRadius: BorderRadius.only(
                                          topLeft: Radius.circular(12.r), 
                                          topRight: Radius.circular(12.r)
                                        ),
                                        gradient: const LinearGradient(
                                          begin: Alignment.topCenter,
                                          end: Alignment.bottomCenter,
                                          colors: [
                                            Color(0xffBAE5C7), 
                                            Colors.white
                                          ]
                                        ),
                                      ),
                                      padding: EdgeInsets.symmetric(vertical: 12.w, horizontal: 16.w),
                                      child: Text('大名单', style: TextStyle(fontSize: 17.sp, fontWeight: FontWeight.w500),),
                                    ),
                
                                    Container(
                                      width: double.infinity,
                                      decoration: BoxDecoration(
                                        color: Colors.white,
                                        borderRadius: BorderRadius.only(
                                          bottomLeft: Radius.circular(12.r), 
                                          bottomRight: Radius.circular(12.r)
                                        ),
                                      ),
                                      margin: EdgeInsets.only(top: 44.w),
                                      padding: EdgeInsets.only(top: 15.w, bottom: 40.w),
                                      child: (gameRecordDescController.historyMatchRecordModel?.signUpUserVOS?.length ?? 0) == 0 ? const SizedBox(
                                        child: CustomEmpty(
                                          text: '暂无球员报名',
                                        ),
                                      ) : ListView.separated(
                                        itemCount: gameRecordDescController.historyMatchRecordModel?.signUpUserVOS?.length ?? 0,
                                        physics: const NeverScrollableScrollPhysics(),
                                        shrinkWrap: true,
                                        separatorBuilder: (context, index) {
                                          return Container(
                                            width: double.infinity,
                                            height: 0.3.w,
                                            color: const Color(0xffDDDDDD),
                                            margin: EdgeInsets.symmetric(horizontal: 16.w),
                                          );
                                        },
                                        itemBuilder: (BuildContext context, int index) {
                                          return Container(
                                            padding: EdgeInsets.symmetric(vertical: 10.w, horizontal: 16.w),
                                            child: Row(
                                              children: [
                                                GestureDetector(
                                                  behavior: HitTestBehavior.translucent,
                                                  onTap: () {
                                                    if (gameRecordDescController.historyMatchRecordModel?.signUpUserVOS?[index].imgUrl != null) {
                                                      Get.to(
                                                        ExtendedImageSlideWidget(
                                                          url: "${gameRecordDescController.historyMatchRecordModel?.signUpUserVOS?[index].imgUrl ?? ''}",
                                                        ),
                                                        opaque: false
                                                      );
                                                    }
                                                  },
                                                  child: Container(
                                                    width: 48.w,
                                                    height: 48.w,
                                                    decoration: BoxDecoration(
                                                      borderRadius: BorderRadius.circular(48.r),
                                                      color: const Color(0xffEDF5F1)
                                                    ),
                                                    padding: EdgeInsets.all(3.w),
                                                    child: ClipRRect(
                                                      borderRadius: BorderRadius.circular(48.r),
                                                      child: Image.network(gameRecordDescController.historyMatchRecordModel?.signUpUserVOS?[index].imgUrl ?? '', fit: BoxFit.fill,),
                                                    ),
                                                  ),
                                                ),
                
                                                SizedBox(width: 8.w,),
                
                                                Expanded(
                                                  child: Column(
                                                    crossAxisAlignment: CrossAxisAlignment.start,
                                                    children: [
                                                      SizedBox(
                                                        child: Row(
                                                          children: [
                                                            Flexible(
                                                              child: Text(gameRecordDescController.historyMatchRecordModel?.signUpUserVOS?[index].nickname ?? '', style: TextStyle(fontSize: 15.sp, fontWeight: FontWeight.w500, color: const Color(0xff333333)), maxLines: 1, overflow: TextOverflow.ellipsis,),
                                                            ),
                                                            SizedBox(width: 8.w,),
                                                            SizedBox(
                                                              width: 16.w,
                                                              height: 16.w,
                                                              child: Image.asset(StrUtils.getSexImage(gameRecordDescController.historyMatchRecordModel?.signUpUserVOS?[index].sex ?? 0)),
                                                            )
                                                          ],
                                                        ),
                                                      ),
                
                                                      SizedBox(height: 4.w,),
                
                                                      SizedBox(
                                                        width: 69.w,
                                                        height: 18.w,
                                                        child: Image.asset(StrUtils.getLevelImage(gameRecordDescController.historyMatchRecordModel?.signUpUserVOS?[index].grade ?? 1)),
                                                      )
                                                    ],
                                                  ),
                                                ),
                
                                                Container(
                                                  constraints: BoxConstraints(
                                                    maxHeight: 18.w,
                                                    maxWidth: 140
                                                  ),
                                                  child: ListView.separated(
                                                    itemCount: gameRecordDescController.historyMatchRecordModel?.signUpUserVOS?[index].goodPosition?.length ?? 0,
                                                    shrinkWrap: true,
                                                    scrollDirection: Axis.horizontal,
                                                    physics: const BouncingScrollPhysics(),
                                                    separatorBuilder: (context, subIndex) {
                                                      return SizedBox(width: 4.w,);
                                                    },
                                                    itemBuilder: (BuildContext context, int subIndex) {
                                                      return Container(
                                                        decoration: BoxDecoration(
                                                          color:  StrUtils.getColorByCode(gameRecordDescController.historyMatchRecordModel?.signUpUserVOS?[index].goodPosition?[subIndex].positionSortCode).withOpacity(0.1),
                                                          borderRadius: BorderRadius.circular(15.r)
                                                        ),
                                                        padding: EdgeInsets.symmetric(vertical: 2.w, horizontal: 9.w),
                                                        child:  Text(gameRecordDescController.historyMatchRecordModel?.signUpUserVOS?[index].goodPosition?[subIndex].subdivisionPosition ?? '', style: TextStyle(fontSize: 10.sp, color: StrUtils.getColorByCode(gameRecordDescController.historyMatchRecordModel?.signUpUserVOS?[index].goodPosition?[subIndex].positionSortCode))),
                                                      );
                                                    },
                                                  ),
                                                )
                                              ],
                                            ),
                                          );
                                        },
                                      ),
                                    )
                                  ],
                                ),
                                
                              ],
                            ),
                          ),
                        )
                      ],
                    )
                  ),
                );
              }
            ),
          ),


          Positioned(
            top: 0,
            left: 0,
            right: 0,
            child: PreferredSize(
              preferredSize: const Size.fromHeight(kToolbarHeight), // 设置 AppBar 的高度为 0
              child: AppBar(
                elevation: 0, 
                backgroundColor: Colors.transparent,
                iconTheme: const IconThemeData(
                  color: Colors.white
                ),
                title: Text('比赛记录详情', style: TextStyle(fontSize: 16.sp, color: Colors.white),),
                centerTitle: true,
              ),
            ),
          )
        ],
      ),
    );
  }

  _typeColumnIconTextWidget({
    String text = '',
    String icon = ''
  }) {
    return Container(
      padding: EdgeInsets.symmetric(vertical: 5.w),
      child: Row(
        children: [
          SizedBox(
            width: 16.w,
            height: 16.w,
            child: Image.asset(icon),
          ),
          SizedBox(width: 8.w,),
          SizedBox(
            child: Text(text, style: TextStyle(fontSize: 12.sp, color: const Color(0xff666666)),),
          )
        ],
      ),
    );
  }


  _typeRowIconTextWidget({
    String text = '',
    String icon = ''
  }) {
    return Container(
      padding: EdgeInsets.symmetric(vertical: 5.w),
      child: Row(
        children: [
          SizedBox(
            width: 16.w,
            height: 16.w,
            child: Image.asset(icon),
          ),
          SizedBox(width: 8.w,),
          SizedBox(
            child: Text(text, style: TextStyle(fontSize: 12.sp, color: const Color(0xff666666)),),
          )
        ],
      ),
    );
  }

  // 比赛信息
  _gameRecordInfoWidget(GameRecordDescController gameRecordDescController) {
    return Stack(
      clipBehavior: Clip.none,
      alignment: Alignment.center,
      children: [
        Container(
          width: double.infinity,
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(12.r),
            color: Colors.white
          ),
          child: Column(
            children: [
              SizedBox(height: 52.w,),

              SizedBox(
                child: Text(gameRecordDescController.historyMatchRecordModel?.clubName ?? '', style: TextStyle(fontSize: 17.sp, fontWeight: FontWeight.bold, color: const Color(0xff333333)),),
              ),

              SizedBox(height: 12.w,),

              Container(
                margin: EdgeInsets.symmetric(horizontal: 15.w),
                padding: EdgeInsets.symmetric(vertical: 8.w, horizontal: 14.w),
                decoration: BoxDecoration(
                  color: const Color(0xffF9F9F9),
                  borderRadius: BorderRadius.circular(8.r)
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    // _typeColumnIconTextWidget(text: gameRecordDescController.historyMatchRecordModel?.courtName ?? '', icon: 'assets/images/game_local.png'),

                    // _typeColumnIconTextWidget(text: gameRecordDescController.historyMatchRecordModel?.matchTime ?? '', icon: 'assets/images/game_time.png'),

                    // SizedBox(
                    //   child: Row(
                    //     mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    //     children: [
                    //       _typeRowIconTextWidget(text: gameRecordDescController.historyMatchRecordModel?.matchRulesName ?? '', icon: 'assets/images/game_rule.png'),
                    //       _typeRowIconTextWidget(text: gameRecordDescController.historyMatchRecordModel?.isPunishment == 'false' ? '报水罚款' : '无报水惩罚', icon: 'assets/images/game_warning.png'),
                    //       _typeRowIconTextWidget(text: '${gameRecordDescController.historyMatchRecordModel?.siteFees ?? 0}元', icon: 'assets/images/icon_sitefee.png')
                    //     ],
                    //   ),
                    // )

                    _typeColumnIconTextWidget(text: gameRecordDescController.historyMatchRecordModel?.courtName ?? '', icon: 'assets/images/game_local.png'),

                    SizedBox(height: 4.w,),

                    SizedBox(
                      child: Row(
                        children: [
                          _typeRowIconTextWidget(text: gameRecordDescController.historyMatchRecordModel?.matchTime ?? '', icon: 'assets/images/game_time.png'),

                          SizedBox(width: 10.w,),

                          _typeRowIconTextWidget(text: gameRecordDescController.historyMatchRecordModel?.isPunishment == 'true' ? '报水罚款' : '无报水惩罚', icon: 'assets/images/game_warning.png'),
                        ],
                      ),
                    ),

                    SizedBox(height: 4.w,),

                    SizedBox(
                      child: Row(
                        children: [
                          _typeRowIconTextWidget(text: gameRecordDescController.historyMatchRecordModel?.matchRulesName ?? '', icon: 'assets/images/game_rule.png'),

                          SizedBox(width: 30.w,),

                          _typeRowIconTextWidget(text: gameRecordDescController.historyMatchRecordModel?.jerseyColor == null || gameRecordDescController.historyMatchRecordModel?.jerseyColor == '' ? '未知' : gameRecordDescController.historyMatchRecordModel?.jerseyColor ?? '未知', icon: 'assets/images/qyys.png'),

                          SizedBox(width: 30.w,),

                          _typeRowIconTextWidget(text: gameRecordDescController.historyMatchRecordModel?.opponent ?? '未知', icon: 'assets/images/vs.png'),
                        ],
                      ),
                    ),

                    if (gameRecordDescController.historyMatchRecordModel?.remark != null) ...[
                      SizedBox(height: 8.w,),

                    
                      Container(    
                        width: double.infinity,
                        height: 0.5.w,
                        color: const Color(0xffDDDDDD),
                      ),

                      SizedBox(height: 8.w,),

                      SizedBox(
                        child: Text(gameRecordDescController.historyMatchRecordModel?.remark ?? '', style: TextStyle(fontSize: 12.sp, fontWeight: FontWeight.w500, color: const Color(0xff999999)),),
                      )
                    ]


                  ],
                ),
              ),

              SizedBox(height: 15.w,)
            ],
          ),
        ),

        Positioned(
          top: -40.w,
          child: GestureDetector(
            behavior: HitTestBehavior.translucent,
            onTap: () {
              if (gameRecordDescController.historyMatchRecordModel?.clubImage != null) {
                Get.to(
                  ExtendedImageSlideWidget(
                    url: "${gameRecordDescController.historyMatchRecordModel?.clubImage ?? ''}",
                  ),
                  opaque: false
                );
              }
            },
            child: Container(
              width: 80.w,
              height: 80.w,
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(10.r),
                boxShadow: const [
                  BoxShadow(
                    color: Color.fromRGBO(0, 0, 0, 0.0800),
                    offset: Offset(0, 2),
                    blurRadius: 4,
                  ),
                ],
              ),
              padding: EdgeInsets.all(3.w),
              child: ClipRRect(
                borderRadius: BorderRadius.circular(10.r),
                child: Image.network(gameRecordDescController.historyMatchRecordModel?.clubImage ?? '', fit: BoxFit.fill,),
              ),
            ),
          )
        )
      ],
    );
  }

}